package com.it.xyx.controller;

import com.it.xyx.api.PayFeignApi;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2024/8/14 17:17
 * @Created by 潜伏Sir
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    PayFeignApi payFeignApi;

    @GetMapping("/getPay/{id}")
    public String getPayInfo(@PathVariable("id") Integer id) {
        System.out.println("用户服务-------支付微服务远程调用，查询订单支付流水信息");
        return payFeignApi.getPay();
    }

    @GetMapping("/circuit/{id}")
    //fallbackMethod的值是降级方法 name是唯一
    @CircuitBreaker(name = "cloud-server-pay", fallbackMethod = "myCircuitFallback")
    //隔离
    @Bulkhead(name = "cloud-server-pay", fallbackMethod = "myBulkhead",type = Bulkhead.Type.SEMAPHORE)
    //限流
    @RateLimiter(name = "cloud-server-pay", fallbackMethod = "myRateLimiter")
    public String circuit(@PathVariable("id") Integer id) {
        return payFeignApi.myCircuit(id);
    }

    public String myCircuitFallback(Integer id, Throwable throwable){
        return "系统繁忙，请稍后再试~";
    }

    public String myBulkhead(Integer id, Throwable throwable){
        return "系统繁忙，请稍后再试~ from bulkhead";
    }

    public String myRateLimiter(Integer id, Throwable throwable){
        return "你被限流了，禁止访问/(ㄒoㄒ)/~~";
    }

}
